package front

import (
	"fmt"
	"time"
	"xmshop-gin/models"

	"github.com/gin-gonic/gin"
)

type IndexController struct {
	BaseController
}

func (con IndexController) Index(c *gin.Context) {
	timeStart := time.Now().UnixNano()
	// 1、获取轮播图数据
	focusList := []models.Focus{}
	if hasFocusList := models.CacheDb.Get("focusList", &focusList); !hasFocusList {
		models.DB.Where("status=1 AND focus_type=1").Find(&focusList)
		models.CacheDb.Set("focusList", &focusList, 60*60)
	}

	// 手机
	phoneList := []models.Goods{}
	if hasPhoneList := models.CacheDb.Get("phoneList", &phoneList); !hasPhoneList {
		phoneList = models.GetGoodsByCategory(1, "best", 8)
		models.CacheDb.Set("phoneList", &phoneList, 60*60)
	}

	// 配件
	otherList := []models.Goods{}
	if hasOtherList := models.CacheDb.Get("otherList", &otherList); !hasOtherList {
		otherList = models.GetGoodsByCategory(9, "all", 1)
		models.CacheDb.Set("otherList", &otherList, 60*60)
	}

	timeEnd := time.Now().UnixNano()

	fmt.Printf("---执行时间：%v 毫秒---", (timeEnd-timeStart)/1000000)

	con.Render(c, "front/index/index.html", gin.H{
		"focusList": focusList,
		"phoneList": phoneList,
		"otherList": otherList,
	})
}
